<template>
	<view>
		<commonList v-for="(item,index) in list" :item="item" :index="index"></commonList>
	</view>
</template>

<script>
	import commonList from '../../components/common/common-list.vue'
	export default {
		components:{
			commonList
		},
		data() {
			return {
				list:[]
			}
		},
		onLoad() {
			let list = uni.getStorageSync('historylist')
			if(list){
				this.list = JSON.parse(list)
			}
			//监听点赞评论
			uni.$on('updateFolloworSurpport', (e) => {
				console.log(e)
				switch (e.type) {
					case 'follow':
						this.follow(e.user_id)
						break;
					case 'support':
						this.dosupport(e.data)
						break;
					default:
						break;
				}
			})
			// 监听评论数
			uni.$on('updatecommoncount', (e) => {
				this.list.forEach(v => {
					if (v.id === e.id) {
						v.comment_count = e.count
					}
				})
			})
		},
		onUnload() {
			uni.$off('updateFolloworSurpport', (e) => {})
			uni.$off('updatecommoncount', (e) => {})
		},
		methods: {
			//关注
			follow(e) {
				this.list.forEach(v => {
					if (v.user_id === e) {
						v.isFollow = true
					}
				})
				uni.showToast({
					title: '关注成功'
				});
			},
			//点赞取消点赞操作
			dosupport(e) {
				this.list.forEach(item => {
					if (item.id === e.id) {
						if (item.support.type === '') {
							item.support.type = e.type
							item.support[e.type + '_count']++
						} else if (item.support.type === 'support' && e.type === 'unsupport') {
							item.support.support_count--
							item.support.unsupport_count++
						} else if (item.support.type === 'unsupport' && e.type === 'support') {
							item.support.support_count++
							item.support.unsupport_count--
						}
						item.support.type = e.type
					}
				})
				uni.showToast({
					title: e.type === 'support' ? '喜欢' : "不喜欢"
				});
			},
		}
	}
</script>

<style>

</style>
